<?php
class Controller_Admin_Rule extends Controller_Template
{

	public function __construct($request, $response)
	{
		parent::__construct($request, $response);
	
		$this->template = ('smarty:admin/rule/index');
	}

	public function action_index()
	{
		$roles	= User_Role_RoleDefine::get_all_names();

		$function_name	= $this->request->controller();
	
		$this->template->set('function_name', $function_name);
		$this->template->set('data', $roles);
		$this->template->set('page_title', '角色列表');
		$this->response->body($this->template->render());
	}

	public function action_rule_list(){

		$function_name	= $this->request->controller();

		$this->template->set('function_name', $function_name);
		$str	= $this->role_resource_select();
		$this->template->set_filename('smarty:admin/rule/add_rules');
		$this->template->set('str', $str);
		$this->template->set('page_title', '角色资源操作');
		$this->response->body($this->template->render());
	}

	public function action_add_rules(){

		$rules	= $this->request->post();

		if($rules['action'] == 'add'){

			foreach($rules as $key => $val){

				if(strpos($key, 'chk_') === 0){

					$this->add_role_rule($val);	
				}
			}
		}

		$url	= '/rule/rule_list';
		$this->request->redirect($url, $code='302');
	}

	protected function add_role_rule($rules){

		if(empty($rules))
			return false;

		$data	= explode('_',$rules);
		
		$resource_id	= $data[0];
		$role_id		= $data[1];
		
		$has	= $this->has_rule($resource_id, $role_id);

		if(! $has)
			$this->add_data($resource_id, $role_id);
		return true;
	}

	private function add_data($resource_id, $role_id)
	{
		$resource_id	= intval($resource_id);

		$role_id		= intval($role_id);

		$type	= 'allow';	

		$rs		= User_Role_Rule::insert_rule($type, $role_id, $resource_id);
	
		return $rs;
	}

	protected function role_resource_select()
	{
		$str = '<div class="city_selected" id="city_selected">';
		$str .= '</div>';
		$str .= '<div class="city_panel"><div class="city_divide"><div class="city">&nbsp;</div>';
		
		$roles	= User_Role_RoleDefine::get_all_names();
		
		foreach($roles as $key => $value)
		{
			$str .= '<div id="channel_'.$value['role_name'].'">'.$value['description'].'</div>';
		}
		$str .= '<div style="float:none;clear:both;"></div></div>';
		
		$resources	= User_Role_Resource::get_all_resources();
		if(!empty($resources))
		{
			$max_length = count($resources);
			foreach($resources as $key => $item)
			{
				if($key + 1 == $max_length)
					$str .= '<div class="city_divide_bottom">';
				else
					$str .= '<div class="city_divide">';
				$str .= '<div class="city" id="city_'.$item['id'].'">'.$item['name'].'</div>';
				foreach($roles as $k => $val)
				{
					$checked = ($this->has_rule($item['id'], $val['id'])) ? ' checked="true" disabled="true"':'';
					$str .= '<div><input type="checkbox" value="'.$item['id'].'_'.$val['id'].'_'.$val['role_name'].'" '.$checked.' id="chk_'.$item['id'].'_'.$k.'" name="chk_'.$item['id'].'_'.$k.'" class="city_chk"/></div>';
				}
				$str .= '<div style="float:none;clear:both;"></div></div>';
			}
		}
		$str .= '<div style="float:none;clear:both;"></div></div>';
		return $str;
	}

	private function has_rule($resource_id, $role_id)
	{
	
		$res_id		= intval($resource_id);
		$role_id	= intval($role_id);

		if($res_id <=0 || $role_id <=0)
			return '';

		$rs	= User_Role_Rule::has_rule($resource_id, $role_id);

		return $rs;
	}
}

?>
